﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ResourceManagement.Client;
using Microsoft.ResourceManagement.ObjectModel;

namespace Ensynch.FIM
{
    public class FimWsProxy
    {
        private DefaultClient defaultClient;
        public DefaultClient DefaultClient
        {
            get
            {
                return defaultClient;
            }
        }

        private static FimWsProxy defaultInstance;
        public static FimWsProxy DefaultInstance
        {
            get
            {
                if (defaultInstance == null)
                {
                    defaultInstance = new FimWsProxy();
                }
                return defaultInstance;
            }
        }

        private FimWsProxy()
        {
            defaultClient = new DefaultClient();
        }

        public FimWsProxy(DefaultClient client)
        {
            defaultClient = client;
        }

        public RmResource GetObject(
            string reference, List<string> attributes)
        {
            string guid = FimTools.NormalizeReference(reference);
            IEnumerable<RmResource> objects = null;

            string xpath = string.Format(FimTools.XPATH_OBJECT_ID, reference);

            if (attributes == null)
            {
                objects = defaultClient.Enumerate(xpath);
            }
            else
            {
                objects = defaultClient.Enumerate(xpath, attributes.ToArray());
            }

            foreach (RmResource obj in objects)
            {
                return obj;
            }

            return null;
        }

        public RmResource GetObject(
            string reference, bool allAttributes)
        {
            if (allAttributes)
            {
                return GetObject(reference, null);
            }
            else
            {
                List<string> selection = new List<string>
                {
                    FimTools.ATTRIBUTE_DISPLAY_NAME,
                    FimTools.ATTRIBUTE_OBJECT_ID
                };
                return GetObject(reference, selection);
            }
        }

        public RmResource GetObject(
            Guid guid, List<string> attributes)
        {
            return GetObject(guid.ToString(), attributes);
        }

        public RmResource GetObject(
            Guid guid, bool allAttributes)
        {
            return GetObject(guid.ToString(), allAttributes);
        }

        public IEnumerable<RmResource> GetObjects(
            string xpath, List<string> attributes)
        {
            IEnumerable<RmResource> objects = null;
            if (attributes == null)
            {
                objects = defaultClient.Enumerate(xpath);
            }
            else
            {
                objects = defaultClient.Enumerate(xpath, attributes.ToArray());
            }
            return objects;
        }

        public IEnumerable<RmResource> GetObjects(
            string xpath, bool allAttributes)
        {
            if (allAttributes)
            {
                return GetObjects(xpath, null);
            }
            else
            {
                List<string> selection = new List<string>
                {
                    FimTools.ATTRIBUTE_DISPLAY_NAME,
                    FimTools.ATTRIBUTE_OBJECT_ID
                };
                return GetObjects(xpath, selection);
            }
        }

    }
}
